package com.situ.mall.job;

import com.situ.mall.constant.RedisConstants;
import com.situ.common.util.AliOSSUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.util.ObjectUtils;

import java.util.Set;

@Configuration
public class DeleteImageJob {
    @Autowired
    private RedisTemplate redisTemplate;

    @Scheduled(cron = "0 0/2 * * * ?")
    public void deleteImage() {
        System.out.println("DeleteImageJob.deleteImage");
        //根据Redis中保存的两个set集合进行差值计算，获得垃圾图片名称集合
        Set<String> set = redisTemplate.opsForSet().difference(RedisConstants.UPLOAD_IMAGE, RedisConstants.UPLOAD_IMAGE_TO_DB);
        if (!ObjectUtils.isEmpty(set)) {
            for (String imageName : set) {
                //删除阿里云oss中垃圾图片
                AliOSSUtils.deleteFile(imageName);
                //从Redis集合中删除图片名称
                redisTemplate.opsForSet().remove(RedisConstants.UPLOAD_IMAGE, imageName);
                System.out.println("删除图片：" + imageName);
            }
        }

    }
}